package com.example.natatorium_management.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.natatorium_management.entity.Good;
import com.example.natatorium_management.mapper.GoodMapper;
import com.example.natatorium_management.mapper.OrderMapper;
import com.example.natatorium_management.result.restful.R;
import com.example.natatorium_management.result.restful.ResponseEunm;
import com.example.natatorium_management.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.List;


@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements GoodService {
    @Autowired
    GoodMapper goodMapper;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public R<IPage<Good>> queryPage(int current, int size) {
        Page<Good> of = Page.of(current, size);
        return new R<>(ResponseEunm.SUCCESS,goodMapper.queryPage(of));

    }

    @Override
    public R<Good> updateGood(Good good) {
        if (goodMapper.updateGood(good)==1){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.UPDATE_FAIL,null);
    }

    @Override
    public R<Good> addGood(Good good) {
        if(save(good)){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.ADD_FAIL,null);
    }

    @Override
    public R<Good> findGoodById(int GoodId) {
        return new R<>(ResponseEunm.SUCCESS,getById(GoodId));

    }

    @Override
    public R<IPage<Good>> findGood(Good good,int current,int size) {
        if (good.getGoodId()==null){
          return new R<>(ResponseEunm.SUCCESS,goodMapper.findGoodByGoodName(Page.of(current,size),good.getGoodName()));
        }
        return new R<>(ResponseEunm.SUCCESS,goodMapper.findGoodById(Page.of(current,size),good.getGoodId()));
    }

    @Override
    public R<Good> deleteGoodById(int id) {
        if (super.removeById(id)){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return  new R<>(ResponseEunm.DELETE_FAIL,null);
    }

    @Override
    public R<List<Good>> findAllGood(Long orderId) {
        List<Integer> integers = orderMapper.selectAllGoods(orderId);
        return new R<>(ResponseEunm.SUCCESS,super.listByIds(integers));
    }

}
